﻿using System;
using Microsoft.Win32;
using System.IO;
using System.Diagnostics;

namespace UpdateBot2.FileHippo
{
    class FileHippoUpdateChecker
    {

        public const int BROWSER_MODE_DEFAULT = 0;
        public const int BROWSER_MODE_CUSTOM = 1;

        public String BrowserString { get; set; }
        public int BrowserMode { get; set; }
        public Boolean ResultShowAll { get; set; }
        public Boolean ResultDetailed { get; set; }
        public Boolean ResultHideBeta { get; set; }

        private FileInfo _fileHippoExeLocation = null;

        /// <summary>
        /// Default Constructor
        /// </summary>
        public FileHippoUpdateChecker()
        {
            BrowserString = String.Empty;
            BrowserMode = BROWSER_MODE_DEFAULT;
            ResultShowAll = false;
            ResultDetailed = true;
            ResultHideBeta = true;
        }

        /// <summary>
        /// The path of the exe; if it exists.
        /// </summary>
        public FileInfo HippoExePath
        {
            get
            {
                return this._fileHippoExeLocation;
            }
        }

        public Boolean CheckInstallation()
        {
            try
            {
                using (RegistryKey hippoKey = Registry.CurrentUser.OpenSubKey(@"Software\FileHippo.com"))
                {
                    //If key is null, it doesn't exist and nor does the FileHippo Update Checker!
                    if (hippoKey == null)
                    {
                        return false;
                    }

                    String fileLocation = hippoKey.GetValue(null, String.Empty) as String;
                    hippoKey.Close();

                    //Make sure value is present!
                    if (String.IsNullOrWhiteSpace(fileLocation))
                    {
                        return false;
                    }
                    else
                    {
                        //Last check is for the actual EXE.
                        String exePath = Path.Combine(fileLocation, "UpdateChecker.exe");
                        FileInfo exeFile = new FileInfo(exePath);
                        if (exeFile.Exists)
                        {
                            this._fileHippoExeLocation = exeFile;
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                }
            }
            catch (Exception err)
            {
                return false;
            }
        }

        /// <summary>
        /// Loads ALL settings from the registry.
        /// </summary>
        public void Load()
        {
            try
            {
                using (RegistryKey hippoKey = getHippoKey(false))
                {
                    this.BrowserString = hippoKey.GetValue("BrowserString", String.Empty) as String;
                    this.BrowserMode = int.Parse(hippoKey.GetValue("BrowserMode", 0) as String);
                    this.ResultDetailed = parseBoolean(hippoKey.GetValue("ResultDetailed", String.Empty) as String);
                    this.ResultHideBeta = parseBoolean(hippoKey.GetValue("ResultHideBeta", String.Empty) as String);
                    this.ResultShowAll = parseBoolean(hippoKey.GetValue("ResultShowAll", String.Empty) as String);

                    hippoKey.Close();
                }
            }
            catch (Exception err)
            {
                //Handle?
            }
        }

        /// <summary>
        /// Saves ALL settings to the registry.
        /// </summary>
        public void Save()
        {
            using (RegistryKey hippoKey = getHippoKey(true))
            {
                hippoKey.SetValue("BrowserString", this.BrowserString);
                hippoKey.SetValue("ResultShowAll", this.ResultShowAll);
                hippoKey.SetValue("ResultHideBeta", this.ResultHideBeta);
                hippoKey.SetValue("ResultDetailed", this.ResultDetailed);

                hippoKey.Close();
            }
        }

        /// <summary>
        /// Starts the FileHippoUpdate Checker in a new process.
        /// </summary>
        public Process RunInNewProcess()
        {
            ProcessStartInfo sInfo = new ProcessStartInfo(this._fileHippoExeLocation.FullName);
            Process checkerProcess = null;
            try
            {
                checkerProcess = Process.Start(sInfo);
            }
            catch (Exception err)
            {
                //Could be cancelled, etc... just ignore?
                return null;
            }

            return checkerProcess;
        }

        /// <summary>
        /// Parses "True" or "False" values.
        /// Note: If its no equal to "true", its false.
        /// Note: This is case IN-sensitive.
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        private static Boolean parseBoolean(String s)
        {
            return s.ToLower().Equals("true");
        }

        /// <summary>
        /// Fetches the FileHippo registry key where the settings are stored.
        /// </summary>
        /// <returns></returns>
        private static RegistryKey getHippoKey(Boolean writeAble)
        {
            return Registry.CurrentUser.OpenSubKey(@"Software\FileHippo.com\Update Checker", writeAble);
        }
    }
}
